package com.hzqy.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hzqy.commons.log.JLogger;
import com.hzqy.commons.log.LoggerFactory;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.commons.CommonsDao;
import com.hzqy.web.login.LogInfo;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.EpgInfoVo;

public class WebLogFilter implements Filter {
	private final static JLogger LOG = LoggerFactory.getLogger(WebLogFilter.class);

	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		String httpIp = request.getRemoteAddr();// 获取IP
		String current_url = req.getRequestURL().toString();
		String current_par = req.getQueryString();

		if(current_par != null){
			current_url = current_url+"?"+current_par;
		}
		String before_url = req.getHeader("referer");
		String serverId="null";
		String resolution="null";
		String areaId="null";
		String partner = "null";
		EpgInfoVo epgInfoVo = (EpgInfoVo)req.getSession().getAttribute(SessionKey.SESSION_EPGINFO_VO);

		if(req.getSession().getAttribute(SessionKey.SESSION_SERVERID)!=null){
			serverId = (String) req.getSession().getAttribute(SessionKey.SESSION_SERVERID);
		}
		if(req.getSession().getAttribute(SessionKey.SESSION_EPG_RESOLUTION)!=null) {
			resolution = (String) req.getSession().getAttribute(SessionKey.SESSION_EPG_RESOLUTION);
		}
		if(req.getSession().getAttribute(SessionKey.SESSION_AREA)!=null) {
			areaId = (String) req.getSession().getAttribute(SessionKey.SESSION_AREA);
		}
		if(epgInfoVo != null)
			partner = epgInfoVo.getPartner();
		//LOG.debug((current_url.indexOf("/PIC/model/") >0)+"||"+(current_url.indexOf("/PIC/") == -1||current_url.indexOf("/PIC/model/") >0)+""+current_url+"|||");

		// 时间 来源ip serverId 访问路径 上级访问路径
		if(current_url.indexOf("/css/") == -1 && current_url.indexOf("/js/") == -1 &&
				current_url.indexOf("/imgs/") == -1 && current_url.indexOf("/images/") == -1 &&
				(current_url.indexOf("/PIC/") == -1||current_url.indexOf("/PIC/model/") >0)) {//行业需要统计模板访问模板存放在PIC
			//LOG.debug("log="+current_url);
			String logStr = "{" + serverId + "}{" + current_url + "}{" + before_url + "}{" + httpIp + "}" + "{" + resolution + "}" + "{" + areaId + "}" + "{" + partner + "}";
			/*if (before_url.contains(".jsp") || before_url.contains(".html") || current_par.contains("?")) {
				LogInfo logInfo = new LogInfo();
				logInfo.UrlLog(logStr);
			}*/
		}
		chain.doFilter(request, response);
	}

	private void initSystemConfig(FilterConfig config) {
		WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
		Object obj = webApplicationContext.getBean("commonsDao"); //获得springmvc注入的对象
		CommonsDao commonsDao = (CommonsDao)obj;
		boolean flag = SystemConfigUtils.init(commonsDao);
		LOG.debug("LoginFilter--读取sys_config数据库表配置状态："+flag);
		LOG.debug("获取私网地址："+SessionKey.SESSION_SYSTEM_CONFIG.private_net_url.getKey()+"="+SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.private_net_url.getKey()));
	}

	public void init(FilterConfig config) throws ServletException {
		LOG.debug("访问记录统计过滤器启动。。。");
		LOG.debug("WebLogFilter--WEB-EPG过滤器启动...System.getProperty(\"user.dir\")="+System.getProperty("user.dir"));
		LOG.debug("WebLogFilter--ehcache缓存写入磁盘路径："+System.getProperty("java.io.tmpdir"));
		initSystemConfig(config);
		LOG.debug("WebLogFilter--初始化从数据库表sys_config中读取系统配置完成。");
	}

	public void destroy() {
		LOG.debug("访问记录统计过滤器停止。。。");
	}
}
